//
//  YZHomePageViewCtl.m
//  ZhuKuang
//
//  Created by jayce on 17/7/12.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "YZHomePageViewCtl.h"
#import "YZInputOrOutputGasolineViewController.h"
#import "YZHomeInfoApi.h"
#import "YZHomeInfoModel.h"
#import "YHIolInfoListView.h"
#import "YZOilLeftViewController.h"


@interface YZHomePageViewCtl (){
    
    YZHomeInfoModel *yzHomeInfoModel;
}
@property (weak, nonatomic) IBOutlet UILabel *todayRestL;//当前剩余油量
@property (weak, nonatomic) IBOutlet UILabel *todayInL;//今日入油量
@property (weak, nonatomic) IBOutlet UILabel *todayOutL;//今日出油量
@property (weak, nonatomic) IBOutlet UILabel *totalInL;//今日累计入油量
@property (weak, nonatomic) IBOutlet UILabel *totalOutL;//今日累计出油量
@property (weak, nonatomic) IBOutlet UILabel *alarmNumIn;//入油警报
@property (weak, nonatomic) IBOutlet UILabel *alarmNumOut;//出油警报
/**报警列表*/
@property (nonatomic, strong) YHIolInfoListView * iolInfoListView;
@property (nonatomic, copy) NSString *startTime;
@property (nonatomic, copy) NSString *endTime;

- (IBAction)p_currentIolLeft:(id)sender;
- (IBAction)p_todayIolInput:(id)sender;
- (IBAction)p_todayIolOutput:(id)sender;
- (IBAction)p_iolInputAlert:(id)sender;
- (IBAction)p_iolOutputAlert:(id)sender;

@end

@implementation YZHomePageViewCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setRequestStartTime:[CommClass getTodayDateStr] endTime:[CommClass getTodayDateStr]];
}

#pragma mark - 页面显示都要刷新数据
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //默认获取当日数据
    [self getYZHomeInfoWithStartTime:[CommClass getTodayDateStr] endTime:[CommClass getTodayDateStr]];
}
#pragma mark - 网络请求
- (void)getYZHomeInfoWithStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    
    __weak typeof(self) weakself = self;
    NSDictionary *postDic = @{
                              @"startime":startTime,
                              @"endtime" :endTime,
                              @"iolId":kIolID
                              };
    [YZHomeInfoApi getYZHomeInfo:postDic success:^(NSDictionary *response) {
        yzHomeInfoModel = [YZHomeInfoModel mj_objectWithKeyValues:response];
        [weakself p_loadSubviewsWithData:yzHomeInfoModel];
    }];

}

#pragma mark - 页面装载数据
- (void)p_loadSubviewsWithData:(YZHomeInfoModel *)model {
    
    self.todayRestL.text = [NSString stringWithFormat:@"%@L",model.tadyrest];
    self.todayInL.text = [NSString stringWithFormat:@"%@L",model.tadyIn];
    self.todayOutL.text = [NSString stringWithFormat:@"%@L",model.tadyout];
    self.totalInL.text = [NSString stringWithFormat:@"%@L",model.totalIn];
    self.totalOutL.text = [NSString stringWithFormat:@"%@L",model.totalout];
    self.alarmNumIn.text = [NSString stringWithFormat:@"%@次",model.AlarmNumIn];
    self.alarmNumOut.text = [NSString stringWithFormat:@"%@次",model.AlarmNumOut];
}
- (IBAction)p_currentIolLeft:(id)sender {
    
    YZOilLeftViewController *oilLeftCtl = [[YZOilLeftViewController alloc]initWithNibName:@"YZOilLeftViewController" bundle:nil];
    [oilLeftCtl setRequestStartTime:_startTime endTime:_endTime];
    [self.navigationController pushViewController:oilLeftCtl animated:YES];
}

- (IBAction)p_todayIolInput:(id)sender {
    
    YZInputOrOutputGasolineViewController *baseVC = [[YZInputOrOutputGasolineViewController alloc] init];
    baseVC.beginTime = _startTime!=nil ? _startTime:@"";
    baseVC.endTime = _endTime!=nil ? _endTime:@"";
    baseVC.type = @"input";
    [self.navigationController pushViewController:baseVC animated:YES];
    
}

- (IBAction)p_todayIolOutput:(id)sender {
    
    YZInputOrOutputGasolineViewController *baseVC = [[YZInputOrOutputGasolineViewController alloc] init];
    baseVC.beginTime = _startTime!=nil ? _startTime:@"";
    baseVC.endTime = _endTime!=nil ? _endTime:@"";
    baseVC.type = @"output";
    [self.navigationController pushViewController:baseVC animated:YES];
}

- (IBAction)p_iolInputAlert:(id)sender {
    self.iolInfoListView.iolInfoList = [NSMutableArray arrayWithArray:yzHomeInfoModel.inerrorList];
    [self goAlramListView:globalNum0];
}

- (IBAction)p_iolOutputAlert:(id)sender {
    self.iolInfoListView.iolInfoList = [NSMutableArray arrayWithArray:yzHomeInfoModel.outerrorList];
    [self goAlramListView:globalNum1];
}

-(void)goAlramListView:(NSInteger)type
{
    self.iolInfoListView.type = type;
    
    if ([self.iolInfoListView isShow]) {
        [self.iolInfoListView hidden];
    }else{
        [self.view.superview addSubview:_iolInfoListView];
        [self.iolInfoListView show];
    }

}

-(YHIolInfoListView *)iolInfoListView
{
    if (!_iolInfoListView) {
        _iolInfoListView = [YHIolInfoListView viewFromXib];
        _iolInfoListView.frame = CGRectMake(5, kScreenHeight - 78, kScreenWidth-10, 0);
    }
    return _iolInfoListView;
}

- (void)setRequestStartTime:(NSString *)start endTime:(NSString *)end{
    
    self.startTime = start;
    self.endTime = end;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
